package cn.wolfcode.business.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.wolfcode.business.vo.CarPackageAuditVO;
import cn.wolfcode.business.vo.HistoryListVO;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.wolfcode.common.annotation.Log;
import cn.wolfcode.common.core.controller.BaseController;
import cn.wolfcode.common.core.domain.AjaxResult;
import cn.wolfcode.common.enums.BusinessType;
import cn.wolfcode.business.domain.BusCarPackageAudit;
import cn.wolfcode.business.service.IBusCarPackageAuditService;
import cn.wolfcode.common.utils.poi.ExcelUtil;
import cn.wolfcode.common.core.page.TableDataInfo;

/**
 * 套餐审核Controller
 * 
 * @author wolfcode
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/business/audit")
public class BusCarPackageAuditController extends BaseController
{
    @Autowired
    private IBusCarPackageAuditService busCarPackageAuditService;

    /**
     * 查询套餐审核列表
     */
    @PreAuthorize("@ss.hasPermi('business:audit:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusCarPackageAudit busCarPackageAudit)
    {
        startPage();
        List<BusCarPackageAudit> list = busCarPackageAuditService.selectBusCarPackageAuditList(busCarPackageAudit);
        return getDataTable(list);
    }

    /**
     * 导出套餐审核列表
     */
    @PreAuthorize("@ss.hasPermi('business:audit:export')")
    @Log(title = "套餐审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusCarPackageAudit busCarPackageAudit)
    {
        List<BusCarPackageAudit> list = busCarPackageAuditService.selectBusCarPackageAuditList(busCarPackageAudit);
        ExcelUtil<BusCarPackageAudit> util = new ExcelUtil<BusCarPackageAudit>(BusCarPackageAudit.class);
        util.exportExcel(response, list, "套餐审核数据");
    }

    /**
     * 获取套餐审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:audit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(busCarPackageAuditService.selectBusCarPackageAuditById(id));
    }

    /**
     * 新增套餐审核
     */
    @PreAuthorize("@ss.hasPermi('business:audit:add')")
    @Log(title = "套餐审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusCarPackageAudit busCarPackageAudit)
    {
        return toAjax(busCarPackageAuditService.insertBusCarPackageAudit(busCarPackageAudit));
    }

    /**
     * 修改套餐审核
     */
    @PreAuthorize("@ss.hasPermi('business:audit:edit')")
    @Log(title = "套餐审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusCarPackageAudit busCarPackageAudit)
    {
        return toAjax(busCarPackageAuditService.updateBusCarPackageAudit(busCarPackageAudit));
    }

    /**
     * 删除套餐审核
     */
    @PreAuthorize("@ss.hasPermi('business:audit:remove')")
    @Log(title = "套餐审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busCarPackageAuditService.deleteBusCarPackageAuditByIds(ids));
    }

    /*进度查看*/
    @GetMapping("/processImg")
    public void getProcessImg(Long instanceId,HttpServletResponse response) throws IOException {
        InputStream inputStream = busCarPackageAuditService.getProcessImg(instanceId);
        IOUtils.copy(inputStream,response.getOutputStream());
    }

    /*撤销审批*/
    @DeleteMapping(value = "/{id}",headers = "cmd=processCancel")
    public AjaxResult processCancel(@PathVariable Long id){
        return toAjax(busCarPackageAuditService.processCancel(id));
    }

    /*查询历史任务集合*/
    @GetMapping("/historyTasks")
    public AjaxResult getHistoryTasks(Long instanceId){
        List<HistoryListVO> historyListVOS =  busCarPackageAuditService.getHistoryTasks(instanceId);
        return AjaxResult.success(historyListVOS);
    }

    /**
     * 查询套餐审核列表
     */
    @GetMapping("/todoTasks")
    public TableDataInfo todoTasks(BusCarPackageAudit busCarPackageAudit)
    {

        List<BusCarPackageAudit> list = busCarPackageAuditService.todoTasks(busCarPackageAudit);
        return getDataTable(list);
    }

    /**
     * 审批功能
     * @param vo
     * @return
     */
    @PatchMapping(headers = "cmd=audit")
    public AjaxResult audit(@RequestBody CarPackageAuditVO vo){
        busCarPackageAuditService.audit(vo);
        return AjaxResult.success();
    }
}
